//
//  CS_Page_TimeRangeView.m
//  Pods
//
//  Created by 网新中研 on 17/3/1.
//
//

#import "CS_Page_TimeRangeView.h"

#define WIDTH_TIME 90
#define MARGIN_TOP 39.5
#define ITEM_HEGHT 30

#define PLACE_HOLDER_START_DATE @"开始时间"
#define PLACE_HOLDER_END_DATE @"结束时间"

@interface CS_Page_TimeRangeView()

@property (nonatomic, strong) CS_Page_TextFieldView *page_TextFieldView;

@property (nonatomic, strong) UILabel *middleLabel;

@property (nonatomic, strong) UILabel *startLabel;

@property (nonatomic, strong) UILabel *endLabel;

@property (nonatomic, strong) UIButton *startButton;

@property (nonatomic, strong) UIButton *endButton;

@end

@implementation CS_Page_TimeRangeView

+(id)page_TimeRangeViewWithPlaceHolder:(NSString *)placeHolder defaultStartDate:(NSString *)defaultStartDate defaultEndData:(NSString *)defaultEndData{
    
    CS_Page_TimeRangeView *page_TimeRangeView = [CS_Page_TimeRangeView new];
    page_TimeRangeView.startDate = defaultEndData;
    page_TimeRangeView.endDate = defaultEndData;
    page_TimeRangeView.placeHolder = placeHolder;
    page_TimeRangeView.page_TextFieldView = [CS_Page_TextFieldView textFiledWithPlaceHolder:placeHolder];
    
    return page_TimeRangeView;
    
}

-(CGFloat)layoutView:(UIView *)superView primaryY:(CGFloat)primaryY{
    
    CGFloat needHeight = 0.0;
    
    needHeight = [self.page_TextFieldView layoutView:self primaryY:0.0];
    
    UIView *coverView = [[UIView alloc] init];
    coverView.frame = CGRectMake(0, 0, kWindowW, needHeight);
    [self addSubview:coverView];
    
    self.startLabel.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN, MARGIN_TOP, WIDTH_TIME, ITEM_HEGHT);
    self.middleLabel.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN + WIDTH_TIME, MARGIN_TOP, 60, ITEM_HEGHT);
    self.endLabel.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN + WIDTH_TIME + 60, MARGIN_TOP, WIDTH_TIME, ITEM_HEGHT);
    
    [self addSubview:self.startLabel];
    [self addSubview:self.middleLabel];
    [self addSubview:self.endLabel];
    
    self.startButton.frame = self.startLabel.frame;
    self.endButton.frame = self.endLabel.frame;
    [self addSubview:self.startButton];
    [self addSubview:self.endButton];
    
    self.frame = CGRectMake(0, primaryY, kWindowW, needHeight);
    [superView addSubview:self];
    
    return primaryY + needHeight;
}

-(void)reset{
    
    self.startLabel.text = PLACE_HOLDER_START_DATE;
    self.startLabel.textColor = COLOR_TEXT_3;
    self.startLabel.font = [UIFont systemFontOfSize:FONT_SIZE_1];
    self.startDate = @"";
    
    self.endLabel.text = PLACE_HOLDER_END_DATE;
    self.endLabel.textColor = COLOR_TEXT_3;
    self.endLabel.font = [UIFont systemFontOfSize:FONT_SIZE_1];
    self.endDate = @"";
    
}

-(void)startDateClick{
    WS(weakSelf);
    CS_Pop_DateTimePickView *pop_DateTimePickView = [CS_Pop_DateTimePickView instanceDateTimePickViewWithSaveClickHandler:^(NSString *selectDate) {
        weakSelf.startLabel.text = selectDate;
        weakSelf.startLabel.font = [UIFont boldSystemFontOfSize:FONT_SIZE_1];
        weakSelf.startLabel.textColor = COLOR_TEXT_2;
        weakSelf.startDate = selectDate;
    } cancelClickHandler:^{
        weakSelf.startDate = @"";
        weakSelf.startLabel.font = [UIFont systemFontOfSize:FONT_SIZE_1];
        weakSelf.startLabel.text = PLACE_HOLDER_START_DATE;
        weakSelf.startLabel.textColor = COLOR_TEXT_3;
    }];
    [pop_DateTimePickView popShow];
}

-(void)endDateClick{
    WS(weakSelf);
    CS_Pop_DateTimePickView *pop_DateTimePickView = [CS_Pop_DateTimePickView instanceDateTimePickViewWithSaveClickHandler:^(NSString *selectDate) {
        weakSelf.endLabel.text = selectDate;
        weakSelf.endLabel.font = [UIFont boldSystemFontOfSize:FONT_SIZE_1];
        weakSelf.endLabel.textColor = COLOR_TEXT_2;
        weakSelf.endDate = selectDate;
    } cancelClickHandler:^{
        weakSelf.endDate = @"";
        weakSelf.endLabel.font = [UIFont systemFontOfSize:FONT_SIZE_1];
        weakSelf.endLabel.text = PLACE_HOLDER_END_DATE;
        weakSelf.endLabel.textColor = COLOR_TEXT_3;
    }];
    [pop_DateTimePickView popShow];
}

-(UILabel *)middleLabel{
    
    if (_middleLabel == nil) {
        _middleLabel = [[UILabel alloc] init];
        _middleLabel.text = @"至";
        _middleLabel.textColor = COLOR_TEXT_1;
        _middleLabel.font = [UIFont systemFontOfSize:FONT_SIZE_1];
        _middleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _middleLabel;
    
}

-(UILabel *)startLabel{
    
    if (_startLabel == nil) {
        _startLabel = [[UILabel alloc] init];
        _startLabel.text = [self isNullOrEmpty:self.startDate] ? PLACE_HOLDER_START_DATE : self.startDate;
        _startLabel.textColor = COLOR_TEXT_3;
        _startLabel.font = [UIFont systemFontOfSize:FONT_SIZE_1];
        _startLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _startLabel;
    
}

-(UILabel *)endLabel{
    
    if (_endLabel == nil) {
        _endLabel = [[UILabel alloc] init];
        _endLabel.text = [self isNullOrEmpty:self.endDate] ? PLACE_HOLDER_END_DATE : self.endDate;
        _endLabel.textColor = COLOR_TEXT_3;
        _endLabel.font = [UIFont systemFontOfSize:FONT_SIZE_1];
        _endLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _endLabel;
    
}

-(UIButton *)startButton{
    
    if (_startButton == nil) {
        _startButton = [[UIButton alloc] init];
        [_startButton addTarget:self action:@selector(startDateClick) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _startButton;
    
}

-(UIButton *)endButton{
    
    if (_endButton == nil) {
        _endButton = [[UIButton alloc] init];
        [_endButton addTarget:self action:@selector(endDateClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _endButton;
    
}

@end
